﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.McMagWeb.Controllers
{
    [Authorization]
    public class ShopCommodityController : BaseController
    {
        private readonly ShopCommodityBLL _bll = new ShopCommodityBLL();

        #region 列表

        [PermissionFilter]
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);

            var strOrderCondition = string.Empty;
            if (order > 0)
            {
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "Id", "CommodityName", "ShopName", "SoldPrice", "Quantity", "DisplaySort" };
                strOrderCondition = orderByColumns[order] + " " + direction;
            }
            var search = Request["search[value]"];
            var pageIndex = (start / length) + 1;

            TableDataViewDto<ViewShopCommodityDto> theList = _bll.GetPagingList(draw, pageIndex, length, SqlSafe(search),
                strOrderCondition, this.CurrentUser.ShopId, true);

            JsonResult jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        #endregion

        #region 修改

        public ActionResult Update(int id)
        {
            if (id <= 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var model = _bll.GetModelWithExtendInfo(id);
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);

            return View(model);
        }

        [HttpPost]
        public ActionResult Update(ShopCommodity model)
        {
            Result result = new Result() { Status = false, Message = "修改失败" };
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var checkModel = CommodityLibBLL.Instance.GetModel(model.CommodityLibId);
            if (checkModel == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (checkModel.LowerPrice > 0)
            {
                if (model.SoldPrice < checkModel.LowerPrice)
                {
                    result.Message = "零售价格不能小于下线价格:" + checkModel.LowerPrice;
                    return Json(result);
                }
            }
            if (checkModel.CeilingPrice > 0)
            {
                if (model.SoldPrice > checkModel.CeilingPrice)
                {
                    result.Message = "零售价格不能大于上限价格:" + checkModel.CeilingPrice;
                    return Json(result);
                }
            }
            if (checkModel.IsShopCanModifyPrice == false && model.SoldPrice != checkModel.SuggestedPrice)
            {
                result.Message = "此商品不可修改价格";
                return Json(result);
            }
            var tempModel = ShopCommodityBLL.Instance.GetModel(model.Id);
            if (tempModel == null || tempModel.ShopId != CurrentUser.ShopId)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tempModel.SoldPrice = model.SoldPrice;
            tempModel.Quantity = model.Quantity;
            tempModel.DisplaySort = model.DisplaySort;
            tempModel.SellingStatus = model.SellingStatus;
            result.Status = _bll.Update(tempModel);
            result.Message = "修改成功";
            return Json(result);
        }

        #endregion


        /// <summary>
        /// 所有商品数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetCount()
        {
            int shopId = CurrentUser.ShopId;
            var count = ShopCommodityBLL.Instance.GetOneShopAllCommodityList(shopId).Count;
            return Json(count);
        }


        #region 删除

        [HttpPost]
        public JsonResult Delete()
        {
            string ids = Request["doIds"];
            bool result = _bll.DeleteList(ids);

            return Json(result);
        }

        #endregion

        #region 上架/下架

        [HttpPost]
        public JsonResult SetSellStatus(int id)
        {
            if (id <= 0)
            {
                return Json(false);
            }
            var model = _bll.GetModel(id);
            if (model == null)
            {
                return Json(false);
            }
            model.SellingStatus = !model.SellingStatus;
            var result = _bll.Update(model);
            return Json(result);
        }

        /// <summary>
        /// 批量上架下架
        /// </summary>
        /// <param name="id"></param>
        /// <param name="itemIds"></param>
        /// <param name="type">1:上架  0下架</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SetSellStatusIds(string itemIds, int type = 1)
        {
            if (string.IsNullOrEmpty(itemIds))
            {
                return Json(false);
            }
            var result = false;
            var shopCommodityList = ShopCommodityBLL.Instance.GetModelList("Id in (" + itemIds.Trim(',') + ") and ShopId=" + CurrentUser.ShopId);

            foreach (var item in shopCommodityList)
            {
                item.SellingStatus = type == (int)CommonEnum.ShopCommoditySellingStatus.OnSell;
                result = _bll.Update(item);
            }
            return Json(result);
        }

        #endregion
    }
}